package com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationfloor;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.annotations.*;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.nanhai.buddhist.college.framework.common.pojo.PageResult;
import com.nanhai.buddhist.college.framework.common.pojo.CommonResult;
import static com.nanhai.buddhist.college.framework.common.pojo.CommonResult.success;

import com.nanhai.buddhist.college.framework.excel.core.util.ExcelUtils;

import com.nanhai.buddhist.college.framework.operatelog.core.annotations.OperateLog;
import static com.nanhai.buddhist.college.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.nanhai.buddhist.college.module.collegebase.controller.admin.accommodationfloor.vo.*;
import com.nanhai.buddhist.college.module.collegebase.dal.dataobject.accommodationfloor.AccommodationFloorDO;
import com.nanhai.buddhist.college.module.collegebase.convert.accommodationfloor.AccommodationFloorConvert;
import com.nanhai.buddhist.college.module.collegebase.service.accommodationfloor.AccommodationFloorService;

@Api(tags = "管理后台 - 楼层")
@RestController
@RequestMapping("/collegebase/accommodation-floor")
@Validated
public class AccommodationFloorController {

    @Resource
    private AccommodationFloorService accommodationFloorService;

    @PostMapping("/create")
    @ApiOperation("创建楼层")
    public CommonResult<String> createAccommodationFloor(@Valid @RequestBody AccommodationFloorCreateReqVO createReqVO) {
        return success(accommodationFloorService.createAccommodationFloor(createReqVO));
    }

    @PutMapping("/update")
    @ApiOperation("更新楼层")
    public CommonResult<Boolean> updateAccommodationFloor(@Valid @RequestBody AccommodationFloorUpdateReqVO updateReqVO) {
        accommodationFloorService.updateAccommodationFloor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除楼层")
    @ApiImplicitParam(name = "id", value = "编号", required = true, dataTypeClass = String.class)
    public CommonResult<Boolean> deleteAccommodationFloor(@RequestParam("id") String id) {
        accommodationFloorService.deleteAccommodationFloor(id);
        return success(true);
    }

    @GetMapping("/get")
    @ApiOperation("获得楼层")
    @ApiImplicitParam(name = "id", value = "编号", required = true, example = "1024", dataTypeClass = String.class)
    public CommonResult<AccommodationFloorRespVO> getAccommodationFloor(@RequestParam("id") String id) {
        AccommodationFloorDO accommodationFloor = accommodationFloorService.getAccommodationFloor(id);
        return success(AccommodationFloorConvert.INSTANCE.convert(accommodationFloor));
    }

    @GetMapping("/list")
    @ApiOperation("获得楼层列表")
    @ApiImplicitParam(name = "ids", value = "编号列表", required = true, example = "1024,2048", dataTypeClass = List.class)
    public CommonResult<List<AccommodationFloorRespVO>> getAccommodationFloorList(@RequestParam("ids") Collection<String> ids) {
        List<AccommodationFloorDO> list = accommodationFloorService.getAccommodationFloorList(ids);
        return success(AccommodationFloorConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @ApiOperation("获得楼层分页")
    public CommonResult<PageResult<AccommodationFloorRespVO>> getAccommodationFloorPage(@Valid AccommodationFloorPageReqVO pageVO) {
        PageResult<AccommodationFloorDO> pageResult = accommodationFloorService.getAccommodationFloorPage(pageVO);
        List<AccommodationFloorDO> accommodationFloorDOList = pageResult.getList();
        //楼层数优化为按照数字顺序排列
        accommodationFloorDOList.sort((floor1, floor2) -> {
            String floor1Str = formatInviteCode(floor1.getFloorName());
            String floor2Str = formatInviteCode(floor2.getFloorName());
            return Integer.parseInt(floor1Str) - Integer.parseInt(floor2Str);
        });
        return success(AccommodationFloorConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @ApiOperation("导出楼层 Excel")
    @OperateLog(type = EXPORT)
    public void exportAccommodationFloorExcel(@Valid AccommodationFloorExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<AccommodationFloorDO> list = accommodationFloorService.getAccommodationFloorList(exportReqVO);
        // 导出 Excel
        List<AccommodationFloorExcelVO> datas = AccommodationFloorConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "楼层.xls", "数据", AccommodationFloorExcelVO.class, datas);
    }

    @GetMapping("/byBuilding/list")
    @ApiOperation("根据楼栋获得楼层列表")
    public CommonResult<List<AccommodationFloorRespVO>> getAccommodationFloorByBuildingList(@RequestParam("id") String id) {
        List<AccommodationFloorDO> list = accommodationFloorService.getAccommodationFloorByBuildingList(id);
        return success(AccommodationFloorConvert.INSTANCE.convertList(list));
    }

    private String formatInviteCode(String str1) {
        StringBuilder str2 = new StringBuilder();
        for (int i = 0; i < str1.length(); i++) {
            if (Character.isDigit(str1.charAt(i))) {
                str2.append(str1.charAt(i));
            }
        }
        return str2.toString();
    }

}
